<?php
//require_once '../classes/userManager.php';
//require_once '../libs/ihelper.php';
require_once '../config/config.php';

$userManager = new userManager();

$registerSuccess = false;
$submitted = ($_SERVER['REQUEST_METHOD'] == "POST");

if ($submitted) {

// create user object and set POST values to properties
    $user = new User();
    $user->setUsername(htmlentities($_POST['username'], ENT_COMPAT, 'UTF-8'));
    $user->setEmail(htmlentities($_POST['useremail'], ENT_COMPAT, 'UTF-8'));
    $user->setPassword(htmlentities($_POST['password']));
    $user->setConfpass(htmlentities($_POST['confpassword']));

    $registerSuccess = ($userManager->createUserAccount($user));
    if ($registerSuccess) {
        ihelper::RedirectToURL("registersuccess.php");
//        header("Location: registersuccess.php");
//        exit;
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php include('common/headsection.php'); ?>

<!--        <script language=javascript>
            function validate(chk){
                if (chk.checked == 1)
                    return true;
                else {
                    alert("Please accept license agreement.");
                    return false; 
                }
            }
        </script>-->
        <title>iBill - Register</title>
    </head>
    <body>
        <div id="page">
            <div id="wrapper">
                <?php include('common/header.php'); ?>
                <div id="register">
                    <form name="register" id="frmRegister" 
                          action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
                        <fieldset>
                            <legend>User Registration</legend>
                            <table>
<!--                                <tr><td colspan="2">
                                        <div><span class="error">
                                <?php
                                if ($submitted && !$registerSuccess) {
//                                                    echo "<b>Please correct the below items.</b><br/><br/>";
//                                                    echo "<ul>";
//                                                    foreach ($userManager->errs as $err => $errmsg) {
//                                                        echo '<li>' . $errmsg;
//                                                    }
//                                                    echo "</ul><br/>";
                                }
                                ?>
                                            </span></div>
                                    </td>
                                </tr>-->
                                <tr>
                                    <td>
                                        <label for="username">Name
                                            <span style="color:red">*</span>
                                        </label>
                                    </td>
                                    <td>
                                        <?php
                                        echo '<input type="text" id="username" name="username" size="20" ';
                                        if ($submitted) {
                                            echo "value='" . $user->getUsername() . "'";
                                        }
                                        echo '>';

                                        if ($submitted) {
                                            echo "<br/>";
                                            echo ihelper::getErrorMessage($userManager->errs, 'username');
                                        }
                                        ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label for="useremail">Email
                                            <span style="color:red">*</span>
                                        </label>
                                    </td>
                                    <td>
<!--                                        <input type="text" name="useremail" -->
                                        <?php
                                        echo '<input type="text" name="useremail" ';
                                        if ($submitted) {
                                            echo "value='" . $user->getEmail() . "'";
                                        }
                                        echo '>';

                                        if ($submitted) {
                                            echo "<br/>";
                                            echo ihelper::getErrorMessage($userManager->errs, 'email');
                                        }
                                        ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label for="password">Password</label>
                                    </td>
                                    <td>
                                        <input type="password" name="password">
                                        <?php
                                        if ($submitted) {
                                            echo "<br/>";
                                            echo ihelper::getErrorMessage($userManager->errs, 'password');
                                        }
                                        ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label for="confpassword">Retype Password&nbsp;</label>
                                    </td>
                                    <td>
                                        <input type="password" name="confpassword">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="left">
                                        <p><input type="checkbox" name="terms" value="Yes">
                                            I agree to the <b>Terms of Service</b> and <b>Privacy Policy</b>.
                                        </p>
                                        <?php
                                        if ($submitted) {
//                                            echo "<br/>";
                                            echo ihelper::getErrorMessage($userManager->errs, 'terms');
                                        }
                                        ?>

                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="right">
                                        <button class="button" type="submit">Create Account</button>
                                    </td>
                                </tr>
                            </table>
                            <div>
                                <span class="index">Back to
                                    <a href="index.php">Login Page</a>
                                </span>
                            </div>
                        </fieldset>
                    </form>
                </div>
                <?php include('common/footer.php'); ?>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        // place cursor in username field
        document.frmRegister.username.focus();
    </script>
</html>
